import api from "../../utils/apiInstance";
import {getUserInfo} from '../../utils/store';
const app = getApp();
const { htmlDecode} = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    fileUrl: app.globalData.fileUrl,
    baseUrl: app.globalData.baseUrl,

    manager: '',
    teamLeader: '',
    managerScore: '',
    totalScore: '',
    teamMember: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad() {
    this.getRec();
  },

  async getRec() {
    const usr = getUserInfo();
    if(!usr.team){
      wx.showModal({
        title: '提示',
        content: "无班组信息",
        showCancel: false,
        success(res) {
          wx.reLaunch({
            url: '/pages/menu/menu',
          })
        }
      })
      return;
    }
    const date = new Date();
    const year = date.getFullYear();
    const month = date.getMonth() + 1; // 月份从0开始，需要+1
    // 月份补零
    const monthStr = month < 10 ? `0${month}` : month;
    const result = await api.get("/personSafety/getTeamReport",{"dept":usr.siocDepartCode,"team":usr.team,"month":'2025-05'});
    // const result = await api.get("/personSafety/getTeamReport",{"dept":usr.siocDepartCode,"team":usr.team,"month":`${year}-${monthStr}`});
    this.setData({
      manager: result.data.manager,
      teamLeader: result.data.teamLeader,
      managerScore: result.data.managerScore,
      totalScore: result.data.totalScore,
      teamMember: result.data.teamMember,
    })
  },

  async disagree(e) {
    var that = this;
    wx.showModal({
      editable: true, //显示输入框
      placeholderText: '输入不同意原因', //显示输入框提示信息
      success: async function (res) {
        if (res.confirm) { //点击了确认
          if(!res.content){
            wx.showModal({
              title: '提示',
              content: "不同意原因不能为空",
              showCancel: false
            })
            return;
          }
          e.currentTarget.dataset.set.status = 2;
          e.currentTarget.dataset.set.remark = res.content;
          const res2 = await api.post(`/personSafety/audit`,e.currentTarget.dataset.set);
          if (200 == res2.code) {
            wx.showModal({
              title: '提示',
              content: res2.msg,
              showCancel: false,
              success: function (res3) {
                if (res3.confirm) {
                  that.getList();
                }
              }
            });
          } else {
            that.getList();
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            });
          }
        }
      }
    })
  },

  async agree(e) {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '请确认同意',
      showCancel: true,
      success: async function (res) {
        if (res.confirm) {
          e.currentTarget.dataset.set.status = 1;
          const res2 = await api.post(`/personSafety/audit`,e.currentTarget.dataset.set);
          console.log('res2',res2);
          if (200 == res2.code) {
            wx.showModal({
              title: '提示',
              content: res2.msg,
              showCancel: false,
              success: function (res3) {
                if (res3.confirm) {
                  that.getList();
                }
              }
            });
          } else {
            that.getList();
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            });
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})